﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.SM
{
    /// <summary>
    /// MsgManage 的摘要说明。
    /// </summary>
    public partial class MsgManage : Page
    {
        protected override void OnInit(EventArgs e)
        {
            btnReply.Click += new System.EventHandler(btnReply_Click);
            btnNext.Click += new System.EventHandler(btnNext_Click);
            btnRead.Click += new System.EventHandler(btnRead_Click);
            Load += new System.EventHandler(Page_Load);
            base.OnInit(e);
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string Username = SiteContext.Current.UserName.ToString();
                ReadMsg(Username);
                CheckNextMsg(Username);
                btnReply.CommandArgument = "view";
                btnHistory.Attributes["onclick"] = "javascript: mytop=screen.availHeight-330-175;myleft=0;var newhiswin=window.open('../SM/MsgHistory.aspx?Sender=" + Username + "&Receiver=" + txtSender.Text + "','show','height=170,width=350,status=0,toolbar=no,menubar=no,location=no,scrollbars=yes,top='+mytop+',left='+myleft+',resizable=yes');newhiswin.focus();";

            }
        }

        private void CheckNextMsg(string Username)
        {
            int count = SMS.GetNewMsgCount(Username);
            if (count >= 2)
            {
                btnNext.Enabled = true;
            }
            else
            {
                btnNext.Enabled = false;
            }
        }


        private void ReadMsg(string Username)
        {

            txtSender.Enabled = false;
            txtRealName.Enabled = false;
            txtContent.Enabled = false;
            btnReply.Enabled = true;
            btnHistory.Enabled = true;

            SMSInfo sms = SMS.GetNewLocalMsg(SiteContext.Current.UserName);
            if (sms != null)
            {
                txtSender.Text = sms.Sender;
                txtRealName.Text = sms.SenderRealName;
                txtContent.Text = sms.Content;
                txtMsgID.Text = sms.ID.ToString();
            }
        }

        private void btnRead_Click(object sender, System.EventArgs e)
        {
            string Username = SiteContext.Current.UserName.ToString();
            try
            {

                SMS.ReadMsg(txtMsgID.Text.ToString(), Username);
                Response.Write("<script language=javascript>window.opener.location.reload();window.close();</script>");

            }
            catch (Exception ex)
            {
                UDS.Components.Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }

        }

        private void btnNext_Click(object sender, System.EventArgs e)
        {
            string Username = SiteContext.Current.UserName.ToString();
            try
            {
                SMS.ReadMsg(txtMsgID.Text.ToString(), Username);
                Response.Redirect("MsgManage.aspx");
            }
            catch
            {
                Server.Transfer("../Error.aspx");
            }

        }

        private void btnReply_Click(object sender, System.EventArgs e)
        {
            string Username = SiteContext.Current.UserName.ToString();
            lblInstruction.Text = "回复消息";

            #region 更新记录的已读状态

            try
            {
                SMS.ReadMsg(txtMsgID.Text.ToString(), Username);
            }
            catch
            {
                Server.Transfer("../Error.aspx");
            }
            #endregion

            if (btnReply.CommandArgument == "view") //查看消息状态
            {
                btnReply.Text = "发送";
                btnReply.CommandArgument = "reply";
                btnRead.Visible = false;
                btnNext.Visible = false;
                lblSender.Text = "接收者";
                txtSender.Enabled = true;
                txtContent.Enabled = true;
                txtContent.Text = "";
                lblShortCut.Text = "按Ctrl+回车键 发送消息 ";
            }
            else //发送消息状态
            { //sender文本框变为receiver
                SMS.SendMsg(Username, txtSender.Text, txtContent.Text, SMSType.SiteMessage, DateTime.Now, "", 0, 0);
                Response.Write("<script language=javascript>alert('回复成功');window.opener.location.reload();window.close();</script>");

            }

        }
    }
}
